import { defineStore } from 'pinia'

export const useAppStore = defineStore('app', {
  state: () => ({
    settings: {
      theme: 'light',
      primaryColor: '#409EFF',
      autoStart: false,
      minimizeToTray: true,
      language: 'zh-CN'
    },
    devSettings: {
      devMode: false,
      logLevel: 'info',
      apiEndpoint: 'https://api.example.com'
    },
    sidebar: {
      collapsed: false
    },
    systemInfo: {
      os: 'Unknown',
      electronVersion: '',
      chromeVersion: '',
      nodeVersion: ''
    },
    loading: false
  }),
  
  getters: {
    isDarkMode: (state) => state.settings.theme === 'dark',
    isCollapsed: (state) => state.sidebar.collapsed
  },
  
  actions: {
    updateSettings(settings) {
      this.settings = { ...this.settings, ...settings }
    },
    
    updateDevSettings(devSettings) {
      this.devSettings = { ...this.devSettings, ...devSettings }
    },
    
    toggleSidebar() {
      this.sidebar.collapsed = !this.sidebar.collapsed
    },
    
    initSystemInfo() {
      if (window.electronAPI) {
        this.systemInfo.os = window.electronAPI.getPlatform() || 'Unknown'
        window.electronAPI.getAppVersion().then(version => {
          this.systemInfo.electronVersion = version
        })
        this.systemInfo.nodeVersion = window.electronAPI.getNodeVersion ? window.electronAPI.getNodeVersion() : ''
      } else {
        this.systemInfo.nodeVersion = ''
      }
      
      this.systemInfo.chromeVersion = navigator.userAgent.match(/Chrome\/([0-9.]+)/)?.[1] || ''
    },
    
    setLoading(status) {
      this.loading = status
    }
  }
})